﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared;
using Sheego.Service.X400.Job.Shared.BO;
using Sheego.Service.X400.Job.Shared.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl
{
    class UAFIProcess : IUAFIProcess
    {
        private IConsoleProcess ConsoleProcess;
        private IJobConfiguration JobConfiguration;
        private IJobLogger JobLogger;
        private IFileManager FileManager;
        private IUAFIResponseParser ResponseParser;

        public UAFIProcess(IConsoleProcess consoleProcess, IJobConfiguration jobConfiguration, IJobLogger jobLogger,
            IFileManager fileManager, IUAFIResponseParser responseParser)
        {
            ConsoleProcess = consoleProcess;
            JobConfiguration = jobConfiguration;
            JobLogger = jobLogger;
            FileManager = fileManager;
            ResponseParser = responseParser;
        }
        #region IUAFIProcess Members

        public IUAFIResponse Call(IUAFICommand Command)
        {
            if (Command == null)
                throw new ArgumentNullException("Command");

            //Clean up working directory
            FileManager.CleanupTempDirectory();

            //Create CommandFile
            FileManager.CreateCommandTextFile(Command.CommandLine);

            //Execute Process
            var output = new List<String>();
            var error = new List<String>();
            var rc = 99;
            StringBuilder arguments = new StringBuilder();
            arguments.Append("-v");
            arguments.AppendFormat(" -path \"{0}\"", JobConfiguration.UAFIConfigPath);
            arguments.AppendFormat(" {0}", Constants.CommandFileName);
            arguments.AppendFormat(" {0}", Constants.ResponseFileName);
            arguments.AppendFormat(" {0} {1}", JobConfiguration.UAFIMailboxUser, JobConfiguration.UAFIMailboxPassword);

            try
            {
                rc = ConsoleProcess.Call(JobConfiguration.UAFICommandPath, arguments.ToString(), JobConfiguration.UAFIWorkingPath, output, error);

                
                if (rc != 0)
                    throw new SystemException("ConsoleProcess.Call Returns " + rc);


                //Read ResponseFile
                var response = FileManager.ReadResponseTextFile();

                //Parse Response
                return ResponseParser.Parse(response);

            }
            catch (Exception ex)
            {
                JobLogger.Error("ConsoleProcess.Call rc={0}, command={1} arguments={2} WorkingPath={3}",
                      rc, JobConfiguration.UAFICommandPath, arguments, JobConfiguration.UAFIWorkingPath);
                foreach (var info in output)
                    JobLogger.Info(info);
                foreach (var err in error)
                    JobLogger.Error(err);

                return ResponseParser.ParseException(ex);
            }
        }

        #endregion
    }
}
